package com.easy.work.function.mall.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.easy.work.R;
import com.easy.work.function.mall.activity.EditAddressActivity;
import com.easy.work.function.mall.bean.AddressBean;
import com.easy.work.function.mall.bean.DeleteEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhangfeng
 * @date 2017/12/24
 * @desc 选择地址适配器
 */

public class SelectAddressAdapter extends ArrayAdapter<AddressBean.ListEntity> {

    private List<AddressBean.ListEntity> mList;
    private LayoutInflater mInflater;
    private Context mContext;

    public SelectAddressAdapter(Context context, int resource, List<AddressBean.ListEntity> objects) {
        super(context, resource, objects);
        mList = objects;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    /**
     * 刷新数据
     */
    public void refreshData(List<AddressBean.ListEntity> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_select_address, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final AddressBean.ListEntity entity = mList.get(position);
        holder.tvReceiveName.setText(getStr(entity.name));
        holder.tvReceivePhone.setText(getStr(entity.phone));
        if (entity.isDefault == 1) {
            holder.tvReceiveState.setText("默认");
        } else {
            holder.tvReceiveState.setText("");
        }
        holder.tvReceiveAddress.setText(getStr(entity.address));
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new DeleteEvent(entity.consigneeId));
            }
        });
        holder.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditAddressActivity.start(mContext, entity);
            }
        });
        return convertView;
    }

    private String getStr(String str) {
        return TextUtils.isEmpty(str) ? "" : str;
    }

    class ViewHolder {
        @BindView(R.id.checkBox)
        CheckBox checkBox;
        @BindView(R.id.tv_receive_name)
        TextView tvReceiveName;
        @BindView(R.id.tv_receive_phone)
        TextView tvReceivePhone;
        @BindView(R.id.tv_receive_state)
        TextView tvReceiveState;
        @BindView(R.id.tv_receive_address)
        TextView tvReceiveAddress;
        @BindView(R.id.iv_edit)
        ImageView ivEdit;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
